(function () {

    "use strict";

    var api = {
        getMobile: "/api/account/getMobile",
        getSmsUrl: "/api/account/sms",
        doResetPwUrl: "/api/account/doResetpw"
    };

    sm.controller('wx-center-account-password-controller', ['$scope', '$http', 'Toast', 'Modal', function ($scope, $http, Toast, Modal){
        $scope.data = {
            mobile: '',
            code: "",
            password: ""
        };

        $scope.defaultCounts = 90;
        $scope.isCounting = false;
        $scope.sendCodeTxt = '获取验证码';
        $scope.receiveCode = false;

        $scope.getMobile = function () {
            $http.get(api.getMobile).success(function (res) {
                if (res.state) {
                    $scope.data.mobile = res.data;
                } else {
                    window.location.href = '/center/accountEditMobile';
                }
            });
        };

        $scope.resetPw = function () {
            if (!$scope.data.code) {
                Toast.error({title: '请填写验证码'})
                return false;
            }

            if (!$scope.data.password) {
                Toast.error({title: "请填写密码"});
                return false;
            }

            var reg = new RegExp(/^\d{4}$/);
            if (!reg.test($scope.data.code)) {
                Modal.alert({
                    title: "操作提示",
                    content: "验证码为4位有效数字"
                });
                return false;
            }

            if ($scope.data.password.length < 6) {
                Modal.alert({
                    title: "操作提示",
                    content: "密码长度不小于6位"
                });
                return false;
            }

            $http.post(api.doResetPwUrl, {
                mobile: $scope.data.mobile,
                password: $scope.data.password,
                code: $scope.data.code
            }).success(function (res) {
                if (res.state) {
                    doSuccess();
                } else {
                    var err = res.error || "密码设置失败";
                    Toast.error({title: err});
                }
            });
        };

        function doSuccess() {
            Modal.alert({
                title: "操作提醒",
                content: "恭喜您, 密码修改成功!",
                buttons: [{
                    name: "留在本页",
                    event:function (){

                    }
                }, {
                    name: "返回个人中心",
                    event: function () {
                        location.href = '/center';
                    }
                }]
            });
        }

        $scope.getSms = function () {

            var regExp = new RegExp(/^1[34578]\d{9}$/);

            if (!$scope.data.mobile || !regExp.test($scope.data.mobile)) {
                Modal.alert({
                    title: "操作提示",
                    content: "手机号格式填写错误"
                });
                return false;
            }

            $http.get(api.getSmsUrl, {
                params: {
                    type: "resetpw",
                    mobile: $scope.data.mobile
                }
            }).success(function (res) {
                if (res.state) {
                    doCounts();
                    $scope.isCounting = true;
                    $scope.receiveCode = true;
                } else {
                    var err = res.error || '验证码获取失败';
                    Toast.error({title: err})
                }
            });
        };

        function doCounts() {
            var timer = setInterval(function () {
                $scope.defaultCounts -= 1;
                if ($scope.defaultCounts <= 1) {
                    $scope.isCounting = false;
                    $scope.sendCodeTxt = '获取验证码';
                    clearInterval(timer);
                } else {
                    $scope.sendCodeTxt = '等待（'+$scope.defaultCounts+'）秒';
                }
                $scope.$apply();
            }, 1000)
        }

        $scope.getMobile();


    }]);
})();

